<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  users
 */

class m_users_actions_block_EditionAction extends m_backoffice_extends_Action
{
	public function execute()
	{
        $formProvider = f_form_Provider::getInstance();
        $formProvider->setDocumentName("edition")
		             ->setModuleName("users")
		             ->setFormName("edition");

        $values=$formProvider->getParams();
		$user = $this->getUser();

        if($user != null)
        {
            if($formProvider->isSubmitted())
            {
                if($user->getActivity() != 178 && $user->getActivity() != null)
                {
                    $errors = array();
                    if($values['formValues']['societe'] != "")
                    {
                        $user->setFirm($values['formValues']['societe']);
                    }
                    else
                    {
                        $errors['societe'] = "Les champs avec une * sont obligatoires";
                    }

                    if($values['formValues']['raison_sociale'] != "")
                    {
                        $user->setSocial($values['formValues']['raison_sociale']);
                    }
                    else
                    {
                        $errors['raison_sociale'] = "Les champs avec une * sont obligatoires";
                    }

                    if($values['formValues']['siret'] != "")
                    {
                        $user->setSiret($values['formValues']['siret']);
                    }
                    else
                    {
                        $errors['siret'] = "Les champs avec une * sont obligatoires";
                    }

                    if($values['formValues']['fonction'] != "")
                    {
                        $user->setFunction($values['formValues']['fonction']);
                    }
                    else
                    {
                        $errors['fonction'] = "Les champs avec une * sont obligatoires";
                    }
                }

                if($formProvider->isValid())
                {
                    $user->setLastname($values['formValues']['nom'])
                         ->setGender($values['formValues']['civilite'])
                         ->setFirstname($values['formValues']['prenom'])
                         ->setEmail($values['formValues']['mail'])
                         ->setAddress($values['formValues']['adresse'])
                         ->setZip($values['formValues']['cp'])
                         ->setTown($values['formValues']['ville'])
                         ->setPhoneNumber($values['formValues']['telephone'])
                         ->setMobilePhoneNumber($values['formValues']['portable'])
                         ->setFax($values['formValues']['fax'])
                         ->setNewsletter(isset($values['formValues']['newsletter'])?1:0)
                         ->save();
                     if(isset($values['formValues']['newsletter']))
                     {
                         $this->modUserDoList($user);
                     }
                     else
                     {
                         $this->modUserDoList($user,0);
                     }
                     if($values['formValues']['password'] != "" && $values['formValues']['password'] == $values['formValues']['password2'])
                     {
                        $key = m_backoffice_services_BackOfficeService::getKey();
                        $user->setPassword(md5($values['formValues']['password'].$key))->save();
                     }
                     elseif($values['formValues']['password'] != $values['formValues']['password2'])
                     {
                         $this->addToModel("errors", array('password'=>'Les mots de passes sont différents'));
                     }
                    $this->addToModel("modfis", true);
                }
                else
                {
    				$this->addToModel("errors", $formProvider->getErrors());
    			}
            }

            $this->addToModel("user", $user);
           
            $this->setView('success');
        }
	}	
        /*Modify A Dolist User : 1 == subscribed 0 == unsuscribe*/
        public function modUserDoList($user,$type=1)
        {
            $textStatus = "subscribed";
            if($type==0)
            {
                $textStatus ="unsubscribed";
            }
            try
            {
                // WEBSERVICES NEWSLETTER DOLIST
                $client = new SoapClient("http://cgi.dolist.fr/members/service.asmx?WSDL", array ("trace" => 1,	"exceptions" => 0));
                switch($user->getGender())
                {
                    case 'Mme' 	:$dolist_civilite = 1;break;
                    case 'Mlle' :$dolist_civilite = 2;break;
                    default 	:$dolist_civilite = 3;break;
                }
                $XmlContacts = '<DATA ListId="2714">
                                  <CONTACT email="'.$user->getEmail().'" status="'.$textStatus.'">'.
                                  ( $type==0 ? "": '
                                    <FIELDS>
                                      <FIELD>
                                        <NAME>Civilité</NAME>
                                        <VALUE>'.$dolist_civilite.'</VALUE>
                                      </FIELD>
                                      <FIELD>
                                        <NAME>LASTNAME</NAME>
                                        <VALUE>'.$user->getLastname().'</VALUE>
                                      </FIELD>
                                      <FIELD>
                                        <NAME>FIRSTNAME</NAME>
                                        <VALUE>'.$user->getFirstname().'</VALUE>
                                      </FIELD>
                                    </FIELDS>
                                 ').
                                 '</CONTACT>
                                </DATA>';
                
                $tab_contact = array("XmlContacts" => $XmlContacts);
                $add_contact = $client->add_contacts($tab_contact);
                /*$return_xml = $add_contact->add_contactsResult->any;
                $tab_return = $this->my_xml2array($return_xml);
                $tab_return = $tab_return[0];*/
            }
            catch(SoapFault $e){}
        }

	public function validate()
	{
		return $this->getUserContext()->hasRight(array("backoffice_accessBo","users_Edit"));
	}
}